<?php

/**
 * Admin utilities.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class AdminUtils {

    /**
     * The constructor
     */
    public function __construct() {
    }

    /*
     * Query processing in Admin, used for listing table contents.
     *
     * @param Query $query
     * @param string $table
     * @param string $parameterName
     * @param int $parameterValue
     * @return Query
     */
    public static function queryProcessing(
        $query,
        $table,
        $parameterName = NULL,
        $parameterValue = NULL,
        $searchString = NULL
    ) {

        /**
         * Table does not join to any other.
         */
        if ($parameterName == NULL) {
            $query->bindTextString('select', '*');
            $query->bindTextString('join', '');
            $query->bindTextString('where', '');
            $and = 'WHERE';
        }

        /**
         * Table joins to another, and the parameter from the second table is given.
         */
        else if ($parameterValue != NULL) {
            $query->bindTextString('select', '*');
            $query->bindTextString('join', '');
            $query->bindTextString('where', '
                WHERE :table.:name = :value
            ');
            $and = 'AND';
        }

        /**
         * Table joins to another (either directly or via a bridge table),
         * but the parameter from the second table is not given.
         */
        else {
            $query->bindTextString('select', ':table.*, `x`.`name` AS :namnam');
            $bridge = $query->bindTextString('bridge', '
                LEFT OUTER JOIN :namtab AS `z`
                ON :table.`id` = `z`.:table
            ');
            if ($bridge) {
                $query->bindTextString('join', '
                    LEFT OUTER JOIN :name AS `x`
                    ON `x`.`id` = `z`.:name
                ');
            }
            else {
                $query->bindTextString('join', '
                    LEFT OUTER JOIN :name AS `x`
                    ON `x`.`id` = :table.:name
                ');
            }
            $query->bindTextString('where', '');
            $and = 'WHERE';
        }

        $query->bindFieldName('table', $table);

        /**
         * Table is joined to another on a particular parameter.
         */
        if ($parameterName != NULL) {
            $query->bindFieldName('name', $parameterName);
            $query->bindFieldName('namnam', $parameterName . '_name');
            $query->bindFieldName('namtab', $parameterName . '_' . $table);
            $query->bindVariable('value', $parameterValue, FALSE);
        }

        /**
         * Search term is used.
         */
        if ($searchString != NULL) {
            $query->bindTextString('and', $and);
            $query->bindTextString('search', ':table.`name` LIKE "%:search_string%"');
            $query->bindVariable('search_string', $searchString, FALSE, TRUE);
        }

        /**
         * No search term.
         */
        else {
            $query->bindTextString('and', '');
            $query->bindTextString('search', '');
        }

        return $query;
    }

}